#include "PointLight.h"
#include <iostream>
using namespace std;


PointLight::PointLight(Vec3 pos,float ambient ,float diffuse ,float specular )
	:LightBase(pos,ambient,diffuse,specular)
{
	this->pos = pos;
}


PointLight::~PointLight(void)
{
}

void PointLight::calcColor( Vec3& pixelPos,Vec3& normal,COLOR& color )
{
	Vec3 dir = this->pos - pixelPos; 
	//cout << "z:" << this->pos.z << endl;
	float L = dir.length();
	dir /= L;
	float diffuseFactor = -dir.dot(normal) ;
	//cout << diffuseFactor << endl;
	float f = diffuseFactor + 0.5;
	if (diffuseFactor <= 0)
	{
		f = 0.5;
	}
	if(true){//f > 0){
		//COLOR old = color;
		//color = int(int(((0xff0000&color)>>16)*f)&0xff)<<16 |
		//		int(int(((0x00ff00&color)>>8)*f)&0xff)<<8 |
		//		int(int(((0x0000ff&color))*f)&0xff);
		
		//bit op may be slow,try Type cast
		unsigned char* b;
		float r = 0;
		b = (unsigned char *)&color;
		r = b[0]*f;
		if (r > 255)b[0] = 255;else b[0] = r;
		r = b[1]*f;
		if (r > 255)b[1] = 255;else b[1] = r;
		r = b[2]*f;
		if (r > 255)b[2] = 255;else b[2] = r;
		r = b[3]*f;
		if (r > 255)b[3] = 255;else b[3] = r;
	}else{
		color = 0;
	}
	//color *= diffuseFactor;
	//cout << "diffuseFactor:" << diffuseFactor << endl;
}
